package com.srxz.personnelsystem.system.service;

import com.srxz.personnelsystem.system.entity.LoginInfo;
import com.srxz.personnelsystem.system.entity.UserInfo;
import com.srxz.personnelsystem.system.mapper.converter.LoginInfoMapper;
import com.srxz.personnelsystem.system.mapper.converter.UserInfoMapper;
import com.srxz.personnelsystem.system.mapper.dto.UserInfoDTO;
import com.srxz.personnelsystem.system.repository.LoginInfoRepository;
import com.srxz.personnelsystem.system.repository.UserInfoRepository;
import com.srxz.personnelsystem.system.service.mapimpl.UserMapperServiceImpl;
import com.srxz.personnelsystem.util.ReflectFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Optional;

/**
 * @ClassName: BaseService
 * @author: wgchao
 * @createTime: 2019/1/10 12:23 PM
 * @Description: TODO
 */
public class BaseService<T> {
    @Autowired
    private UserInfoRepository userInfoRepository;

    public BaseService<T> extendCommon(T dtoObject){
        String dtoObjectResourceParam = "createUser";//dto对象的原本属性
        String dtoObjectResultParam = "createUserName";//dto对象的转换属性

        String createUserId = String.valueOf(getAttribute(dtoObject, dtoObjectResourceParam));
        Optional<UserInfo> opt = userInfoRepository.findById(createUserId);
        if(opt.isPresent()){
            setAttribute(dtoObject, dtoObjectResultParam,opt.get().getName());
        }

        dtoObjectResourceParam = "deleteUser";//dto对象的原本属性
        dtoObjectResultParam = "deleteUserName";//dto对象的转换属性

        String deleteUserId = String.valueOf(getAttribute(dtoObject, dtoObjectResourceParam));
        opt = userInfoRepository.findById(deleteUserId);
        if(opt.isPresent()){
            setAttribute(dtoObject, dtoObjectResultParam,opt.get().getName());
        }

        dtoObjectResourceParam = "lastUpdateUser";//dto对象的原本属性
        dtoObjectResultParam = "lastupdateUserName";//dto对象的转换属性

        String lastupdateUserId = String.valueOf(getAttribute(dtoObject, dtoObjectResourceParam));
        opt = userInfoRepository.findById(lastupdateUserId);
        if(opt.isPresent()){
            setAttribute(dtoObject, dtoObjectResultParam,opt.get().getName());
        }
        return this;
    }

    /**
     * 获取方法属性
     * @param dtoObject
     * @param attribute
     * @return
     */
    protected String getAttribute(T dtoObject, String attribute) {
        String getMethodName = "get" + attribute.substring(0, 1).toUpperCase() + attribute.substring(1); // 将属性的首字符大写，方便构造get，set方法
        Object value = new ReflectFactory<T>()
                .setObject(dtoObject)
                .setMethodName(getMethodName)
                .invoke();
        if(value != null){
            return value.toString();
        }
        return null;
    }


    /**
     * 设置方法属性
     * @param object
     * @param attribute
     * @param value
     */
    protected void setAttribute(T object, String attribute, Object value) {
        if(value == null){
            return;
        }
        String getMethodName = "get" + attribute.substring(0, 1).toUpperCase() + attribute.substring(1); // 将属性的首字符大写，方便构造get，set方法
        Object attributeValue = new ReflectFactory<T>()
                .setObject(object)
                .setMethodName(getMethodName)
                .invoke();
        if(attributeValue == null){
            String setMethodName = "set" + attribute.substring(0, 1).toUpperCase() + attribute.substring(1); // 将属性的首字符大写，方便构造get，set方法
            new ReflectFactory<T>()
                    .setObject(object)
                    .setMethodName(setMethodName)
                    .setParameterTypes(value.getClass())
                    .setParameter(value)
                    .invoke();
        }
    }
}
